﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;

namespace UncleBobWebService.Database
{
    class PermissionsDBAdapter : IDBAdapter
    {
        public override Message CreateData(Message msg, object data)
        {
            RegisterModel model = (RegisterModel)data;
            Message newMsg = new Message();
            newMsg.Data = model;

            RegisterModel exitingModel = (RegisterModel)SearchForAllData(newMsg).Data;

            if (exitingModel.Username != model.Username)
            {

                using (SqlConnection connection = new SqlConnection(mConnectionString))
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format("INSERT INTO AccessTable (UserName, AccessLevel) VALUES ('{0}', '{1}')"
                        , model.Username, model.Permissions);

                    try
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                        msg = new Message();
                        msg.Result = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            return msg;
        }

        public override Message SearchForSpecificData(Message msg, object data)
        {
            throw new NotImplementedException();
        }

        public override Message SearchForAllData(Message msg)
        {
            RegisterModel model = (RegisterModel)msg.Data;
            RegisterModel newModel = new RegisterModel();
            SqlDataAdapter SqlAdapter;
            DataTable dt = new DataTable();

            msg.Data = newModel;

            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = string.Format("SELECT UserName, AccessLevel FROM AccessTable WHERE UserName = '{0}'" , model.Username);
                SqlAdapter = new SqlDataAdapter(command);

                try
                {
                    connection.Open();
                    SqlAdapter.Fill(dt);
                    connection.Close();

                    if (dt.Rows.Count >= 1)
                    {
                        switch (dt.Rows[0]["AccessLevel"].ToString())
                        {
                            case "Applicant":
                                newModel.Permissions = PermissionTypes.User.Applicant;
                                break;
                            case "StaffingExpert":
                                newModel.Permissions = PermissionTypes.User.StaffingExpert;
                                break;
                            case "HiringManager":
                                newModel.Permissions = PermissionTypes.User.HiringManager;
                                break;
                        }

                        newModel.Username = (string)dt.Rows[0]["UserName"];
                        msg.Result = true;
                    }
   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            return msg;
        }

        public override Message EditSpecificData(Message msg, object data)
        {
            throw new NotImplementedException();
        }

        public override Message DeleteSpecificData(Message msg, object data)
        {
            Message newMsg = new Message();

            SqlDataAdapter SqlAdapter;
            DataTable dt = new DataTable();

            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = string.Format("DELETE FROM AccessTable WHERE UserName = '{0}'", ((RegisterModel)data).Username);
                SqlAdapter = new SqlDataAdapter(command);

                try
                {
                    connection.Open();
                    SqlAdapter.Fill(dt);
                    msg = new Message();
                    msg.Result = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }

            return msg;
        }

        public override Message DeleteAllData(Message msg)
        {
            throw new NotImplementedException();
        }
    }
}
